package com.epalmpay.job;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.epalmpay.entity.YsOilcardRechargeInfo;
import com.epalmpay.enumdef.CommonEnum.RunStatus;
import com.epalmpay.enumdef.CommonEnum.TaskType;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.service.yunstream.IOilcardOrderService;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.SysUtils;
 


@Component
public class OilcardChargeTaskJob {
	private Logger logger = LoggerFactory.getLogger(OilcardChargeTaskJob.class);
	@Resource
	private IOilcardOrderService oilCardOrderService;
	
	@Resource
	private ISysLogService sysLogService;
	/**
	 *6
	 */
	@Scheduled(cron="0 0 6 * * ?")
	//@Scheduled(cron = "0 0/1 * * * ?")
	public void taskOilcardCharge(){
		logger.info("====taskOilcardCharge==============================================================="); 
    	logger.info("进行执行  油卡任务在当天需要执行的订单"); 
    
    	 String currentDate=SysUtils.formatDate("yyyy-MM-dd");
    	List<YsOilcardRechargeInfo> rechargeInfos= oilCardOrderService.findNeedChargeChargeInfo(currentDate);
		for(YsOilcardRechargeInfo chargeInfo:rechargeInfos){
			try {
				Map<String ,String> result=oilCardOrderService.finishOilcardRechargeInfoRecharge(chargeInfo);
			    System.out.println("taskOilcardCharge..."+JSONObject.toJSONString(result));
			  } catch (Exception e) {
				e.printStackTrace();
				StringBuffer sb = new StringBuffer(); 
				try {
					sysLogService.recordSysTaskLog(TaskType.YsOilcardChargeMonthTask.getType(),
							new Date(), RunStatus.Fail.getType(),
							sb.append("充值订单Id:").append(chargeInfo.getId()).append("油卡订单充值，定时充值失败！").toString());
				} catch (Exception e1) {
					e1.printStackTrace();
					continue;
				}
			}	
			try {
				Thread.sleep(10*1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		StringBuffer sb = new StringBuffer(); 
		logger.info(sb.append("计划任务:")
				.append(TaskType.YsOilcardChargeMonthTask.getName())
				.append("统计日期:").append(DateUtil.fomatDate(new Date(), DateUtil.sdfDays)).append("执行成功！").toString());
		try {
			sysLogService.recordSysTaskLog(TaskType.YsOilcardChargeMonthTask.getType(),
					new Date(), RunStatus.Success.getType(),
					sb.append("油卡订单充值").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime)).append("执行成功！").toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
    	logger.info("=======================================taskOilcardCharge end============================="); 
	}
}
